package com.example.phoneyzm.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.example.phoneyzm.mapper.YzmRedisDao;
import com.example.phoneyzm.service.YzmRedisService;
import com.example.phoneyzm.util.ConstantPropertiesUtils;
import com.example.smartcommon.util.R;
import com.example.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class YzmRedisServiceImpl implements YzmRedisService {
    @Autowired
    private YzmRedisDao yzmRedisDao;
    String result = "";

    @Override
    public R sendYzm(User user) throws ClientException {
        String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
        String signName = ConstantPropertiesUtils.SIGN_NAME;
        String templateCode = ConstantPropertiesUtils.TEMPLATE_CODE;
        String product = ConstantPropertiesUtils.PRODUCT;
        String doMain = ConstantPropertiesUtils.DO_MAIN;
        String regionId = ConstantPropertiesUtils.REGION_ID;
        String templateParam = ConstantPropertiesUtils.TEMPLATE_PARAM;

        //不同手机号对应的验证码
        String key = user.getUserPhone();
        //验证码如果不存在
        if (!yzmRedisDao.exists(key)) {
            StringBuilder sb = new StringBuilder();
            //随机数对象
            Random random = new Random();
            //产生4位验证码 .nextInt产生0-10之间的随机数
            for (int i = 0; i < 6; i++) {
                sb.append(random.nextInt(10));
            }
            String yzm = sb.toString();
            Map<String, String> map = new HashMap<>();
            map.put(templateParam, yzm);
            //初始化ACS客户端的 profile 实例
            IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
            //IAcsClient 类型的 client 实例
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysProduct(product);
            request.setSysDomain(doMain);
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", regionId);
            request.putQueryParameter("PhoneNumbers", key);
            request.putQueryParameter("SignName", signName);
            request.putQueryParameter("TemplateCode", templateCode);
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + yzm + "\"}");
            //设置验证码，120是秒为单位，即2分钟
            yzmRedisDao.set(key, yzm, 120);
            client.getCommonResponse(request);
            CommonResponse response = client.getCommonResponse(request);
            result = response.getData();
            return R.ok("验证码发送成功").data("messages", yzm);
        }
        return R.error("验证码不能重复发送");
    }
}
